package com.niit.web.controller;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.niit.bean.User;
import com.niit.service.UserService;
import com.niit.service.impl.UserServiceImpl;

@WebServlet("/UserLoginController")
public class UserLoginController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String pwd = request.getParameter("pwd");
		String ver = request.getParameter("ver").toUpperCase();
		String code = (String) request.getSession().getAttribute("rand");
		OutputStream out = response.getOutputStream();
		if(code!=null&&code.equals(ver)){
			if(username!=null&&pwd!=null){
				UserService us = new UserServiceImpl();
				User user = us.login(username, pwd);
				if(user!=null){
					if(user.isValidation()){
						if(!user.isForbid()){
							out.write("3".getBytes());
						}else{
							request.getSession().setAttribute("user", user);//登录成功，把用户存入session
							out.write("s".getBytes());//登录成功，不容易啊
						}
					}else{
						out.write("2".getBytes());
					}
				}else{
					out.write("1".getBytes());
				}
			}
		}else{
			out.write("0".getBytes());
		}
	}

}
